var mydialog;

$(function(){
	$("#box-tabs").tabs();
	$("#cjzl").css("color","black");
	
	var datepickeroption={
		 dateFormat:"yy-mm-dd",
		 showOn: "button",
		 buttonImage: "../../../Public/images/date_16x16.gif",
		 buttonImageOnly: true,
		 changeMonth:true,
		 changeYear:true
	};
	$( "#qyrq" ).datepicker(datepickeroption);
	$( "#yyrq" ).datepicker(datepickeroption);
	$( "#wsrq" ).datepicker(datepickeroption);
	$( "#jwrq" ).datepicker(datepickeroption);
	$( "#jzwcr" ).datepicker(datepickeroption);
	
	$("#cjzl").click(function(){
		showdiv1();
	});
	
	$("#xgyj").click(function(){
		showdiv2();
	});
	
	$("#other_obj").click(function(){
		$('#wtybh').html("委託約編號");
	});
	
	$("#local_obj").click(function(){
		$('#wtybh').html("<a>*委託約編號</a>");
	});
	
	$("#other").click(function(){
		showdiv6();
	});

});

function checkform(){
	var types=$(".obj_type").get();
	var type="";
	$.each(types,function(key,value){
		if(value["checked"]){
			type=value["value"]
		}
	});
	if(type==='0'){
		$(".tips").html("<span style='font-size:30px;color:green;'>√</span>");
		var error=0;
		var check=$(".check").get();
		$.each(check,function(key,value){
			$(this).attr("style","");
			if(value["value"]==''){
				$(this).attr("style","border:1px solid red");
				error++;
			}
		});
		
		if(error!=0){
			showdiv1();
			$(".tips").html("<span style='font-size:30px;color:red;'>注：紅色邊框的輸入框為必填項</span>");
			return ;
		}
		
		var yjbl=0;
		var jsbl=0;
		for(var i=1;i<=11;i++){
			if($("#yjbl"+i).val()!=''){
				yjbl=yjbl+parseFloat($("#yjbl"+i).val());
			}
			if($("#jsbl"+i).val()!=''){
				jsbl=jsbl+parseFloat($("#jsbl"+i).val());
			}
		}
		
		if(yjbl!=100){
			showdiv2();
			$(".tips").html("<span style='font-size:30px;color:red;'>注：業績比率相加必須為100%</span>");
			return;
		}
		
		if(jsbl!=100){
			showdiv2();
			$(".tips").html("<span style='font-size:30px;color:red;'>注：件數比率相加必須為100%</span>");
			return;
		}
		
		//本店物件检测完毕
		var obj_type=type;
		var qyrq=$("#qyrq").val();
		var seller_ys=$("#seller_ys").val();
		var wtybh=$("#wtybh").val();
		var yyrq=$("#yyrq").val();
		var seller_ss=$("#seller_ss").val();
		var turnover_type=$("#turnover_type").val();
		var wsrq=$("#wsrq").val();
		var buyer_ys=$("#buyer_ys").val();
		var turnover_code=$("#turnover_code").val();
		var jwrq=$("#jwrq").val();
		var buyer_ss=$("#buyer_ss").val();
		var casename=$("#casename").val();
		var cysm=$("#cysm").val();
		var turnover_m=$("#turnover_m").val();
		var dsxm=$("#dsxm").val();
		var cjdj=$("#cjdj").val();
		var dsdz=$("#dsdz").val();
		var total_yj=$("#total_yj").val();
		var dsdh=$("#dsdh").val();
		var jyaqzl=$("#jyaqzl").val();
		var remark1=$("#remark1").val();
		var aqjybzsh=$("#aqjybzsh").val();
		var yj=$("#yj").val();
		var dj=$("#dj").val();
		var zq=$("#zq").val();
		var status_code=$("#status_code").val();
		var yyszl=$("#yyszl").val();
		var yysbh=$("#yysbh").val();
		
		mydialog=art.dialog({title:'请稍后...',lock:true});
		
		$.ajax({
			type:'post',
			url:'turnover_data_save',
			dataType:'json',
			data:{hybh:wtybh,turnover_type:turnover_type,turnover_code:turnover_code,casename:casename,turnover_m:turnover_m,turnover_danjia:cjdj,total_perf:total_yj,trans_sec:jyaqzl,trans_sec_code:aqjybzsh,deposit:yj,earnest:dj,lease:zq,status_code:status_code,yys_type:yyszl,yys_code:yysbh,qy_date:qyrq,yy_date:yyrq,ws_date:wsrq,jf_date:jwrq,cysm:cysm,dsxm:dsxm,dsdz:dsdz,dstel:dsdh,remark:remark1,buyer_ys:buyer_ys,buyer_ss:buyer_ss,seller_ys:seller_ys,seller_ss:seller_ss,type:0,operator:'10'},
			success:save_status
		});
		
	}else if(type==='1'){
		$("#wtybh").css("border","");
		$(".tips").html("<span style='font-size:30px;color:green;'>√</span>");
		var error=0;
		var check=$(".check").get();
		$.each(check,function(key,value){
			$(this).attr("style","");
			if(value['id']==='wtybh'){
			}else if(value["value"]==''){
				$(this).attr("style","border:1px solid red");
				error++;
			}
		});
		
		if(error!=0){
			showdiv1();
			$(".tips").html("<span style='font-size:30px;color:red;'>注：紅色邊框的輸入框為必填項</span>");
			return ;
		}
		
		error=0;
		check=$(".check6").get();
		$.each(check,function(key,value){
			$(this).attr("style","");
			if(value['id']==='wtybh'){
			}else if(value["value"]==''){
				$(this).attr("style","border:1px solid red");
				error++;
			}
		});
		
		if(error!=0){
			showdiv6();
			$(".tips").html("<span style='font-size:30px;color:red;'>注：紅色邊框的輸入框為必填項</span>");
			return ;
		}
		
		var yjbl=0;
		var jsbl=0;
		for(var i=1;i<=11;i++){
			if($("#yjbl"+i).val()!=''){
				yjbl=yjbl+parseFloat($("#yjbl"+i).val());
			}
			if($("#jsbl"+i).val()!=''){
				jsbl=jsbl+parseFloat($("#jsbl"+i).val());
			}
		}
		
		if(yjbl!=100){
			showdiv2();
			$(".tips").html("<span style='font-size:30px;color:red;'>注：業績比率相加必須為100%</span>");
			return;
		}
		
		if(jsbl!=100){
			showdiv2();
			$(".tips").html("<span style='font-size:30px;color:red;'>注：件數比率相加必須為100%</span>");
			return;
		}
		
		//其他物件检测完毕
		var obj_type=type;
		var qyrq=$("#qyrq").val();
		var seller_ys=$("#seller_ys").val();
		var yyrq=$("#yyrq").val();
		var seller_ss=$("#seller_ss").val();
		var turnover_type=$("#turnover_type").val();
		var wsrq=$("#wsrq").val();
		var buyer_ys=$("#buyer_ys").val();
		var turnover_code=$("#turnover_code").val();
		var jwrq=$("#jwrq").val();
		var buyer_ss=$("#buyer_ss").val();
		var casename=$("#casename").val();
		var cysm=$("#cysm").val();
		var turnover_m=$("#turnover_m").val();
		var dsxm=$("#dsxm").val();
		var cjdj=$("#cjdj").val();
		var dsdz=$("#dsdz").val();
		var total_yj=$("#total_yj").val();
		var dsdh=$("#dsdh").val();
		var jyaqzl=$("#jyaqzl").val();
		var remark1=$("#remark1").val();
		var aqjybzsh=$("#aqjybzsh").val();
		var yj=$("#yj").val();
		var dj=$("#dj").val();
		var zq=$("#zq").val();
		var status_code=$("#status_code").val();
		var yyszl=$("#yyszl").val();
		var yysbh=$("#yysbh").val();
		var casename=$('#casename').val();
		var zjwmj=$('#zjwmj').val();
		var jwmp=$('#jwmp').val();
		var fjwmj=$('#fjwmj').val();
		var obj_style=$('#obj_style').val();
		var obj_use=$('#obj_use').val();
		var total_area=$('#total_area').val();
		var kfdzl=$('#kfdzl').val();
		var jwmj=$('#jwmj').val();
		var total_price=$('#total_price').val();
		var unit_area=$('#unit_area').val();
		var carposition_price=$('#carposition_price').val();
		var djmj=$('#djmj').val();
		var jgjg=$('#jgjg').val();
		var zhjg=$('#zhjg').val();
		var name=$('#use_area').val();
		var jzwcr=$('#jzwcr').val();
		var tymj=$('#tymj').val();
		var dxsmj=$('#dxsmj').val();
		var tsgj=$('#tsgj').val();
		var dj_price=$('#dj_price').val();
		var room_style=$('#fang').val()+$('#ting').val()+$('#wei').val();
		
		mydialog=art.dialog({title:'请稍后...',lock:true});
		
		$.ajax({
			type:'post',
			url:'turnover_data_save',
			dataType:'json',
			data:{turnover_type:turnover_type,turnover_code:turnover_code,casename:casename,turnover_m:turnover_m,turnover_danjia:cjdj,total_perf:total_yj,trans_sec:jyaqzl,trans_sec_code:aqjybzsh,deposit:yj,earnest:dj,lease:zq,status_code:status_code,yys_type:yyszl,yys_code:yysbh,qy_date:qyrq,yy_date:yyrq,ws_date:wsrq,jf_date:jwrq,cysm:cysm,dsxm:dsxm,dsdz:dsdz,dstel:dsdh,remark:remark1,buyer_ys:buyer_ys,buyer_ss:buyer_ss,seller_ys:seller_ys,seller_ss:seller_ss,type:0,operator:'10'},
			success:save_status
		});
	}
}

function save_status(data){
	mydialog.close();
	if(data.status===0){
		art.dialog({lock:true,title:'提示',content:'没有权限',ok:function(){}});
	}else if(data.status===2){
		art.dialog({lock:true,title:'提示',content:'非法訪問',ok:function(){}});
	}else if(data.status===3){
		art.dialog({lock:true,title:'提示',content:data.info,ok:function(){}});
	}else if(data.status===1){
		art.dialog({lock:true,title:'提示',width:300,height:100,content:data.info,ok:function(){
			location.reload();
		}});
	}
}

function showdiv1(){
	$("#cjzl").css("color","black");
	$("#xgyj").css("color","#3F90E8");
	$("#cjkh").css("color","#3F90E8");
	$("#skmx").css("color","#3F90E8");
	$("#dszy").css("color","#3F90E8");
	$("#other").css("color","#3F90E8");
	$("#selltable1").css("display","block");
	$("#selltable2").css("display","none");
	$("#selltable3").css("display","none");
	$("#selltable4").css("display","none");
	$("#selltable5").css("display","none");
	$("#selltable6").css("display","none");
}

function showdiv2(){
	$("#cjzl").css("color","#3F90E8");
	$("#xgyj").css("color","black");
	$("#cjkh").css("color","#3F90E8");
	$("#skmx").css("color","#3F90E8");
	$("#dszy").css("color","#3F90E8");
	$("#other").css("color","#3F90E8");
	$("#selltable1").css("display","none");
	$("#selltable2").css("display","block");
	$("#selltable3").css("display","none");
	$("#selltable4").css("display","none");
	$("#selltable5").css("display","none");
	$("#selltable6").css("display","none");
}

function showdiv6(){
	$("#cjzl").css("color","#3F90E8");
	$("#xgyj").css("color","#3F90E8");
	$("#cjkh").css("color","#3F90E8");
	$("#skmx").css("color","#3F90E8");
	$("#dszy").css("color","#3F90E8");
	$("#other").css("color","black");
	$("#selltable1").css("display","none");
	$("#selltable2").css("display","none");
	$("#selltable3").css("display","none");
	$("#selltable4").css("display","none");
	$("#selltable5").css("display","none");
	$("#selltable6").css("display","block");
}

function sell_check_value(obj){
	var value=$(obj).val();
	var i=value.length;
	var re=new RegExp("[0-9]{"+i+"}","g");
	if(value!=''){
		if(!re.test(value)){
			alert("這裡只能為數字哦");
			$(obj).val("");
		}
	}
}